package com.qf.controller;

import com.alibaba.excel.EasyExcel;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.qf.entity.OrderItem;
import com.qf.entity.OrderItemDTO;
import com.qf.listener.OrderItemListener;
import com.qf.service.OrderItemService;
import com.qf.util.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * 天--道--酬--勤
 *
 * @author LiangGaoQiang
 * @ClassName OrderItemController
 * @Date: 2025/5/6 20:02
 * @Description: 描述
 */
@RestController
@RequestMapping("/orderItem")
public class OrderItemController {
    @Autowired
    private OrderItemService orderItemService;

    @GetMapping("/fuzzyQueryOrderItem")
    public R fuzzyQueryOrderItem(OrderItemDTO orderItemDTO) {
        PageHelper.startPage(orderItemDTO.getPageNum(), orderItemDTO.getPageSize());
        List<OrderItem> orderItems = orderItemService.fuzzyQueryOrderItem(orderItemDTO);
        PageInfo<OrderItem> pageInfo = new PageInfo<OrderItem>(orderItems);
        return R.ok(pageInfo);
    }

    @GetMapping("/deleteOrderItem" + "/{id}")
    public R deleteOrderItem(@PathVariable("id") Long id) {
        Boolean result = orderItemService.deleteOrderItem(id);
        if (result) {
            return R.ok();
        }
        return R.error("删除失败");
    }

    @GetMapping("/deleteBatchItems")
    public R deleteBatchBatch(@RequestParam List<Long> ids) {
        Boolean result = orderItemService.deleteBatch(ids);
        if (result) {
            return R.ok();
        }
        return R.error("删除失败");
    }

    @PostMapping("/addOrderItem")
    public R addOrderItem(@RequestBody OrderItem orderItem) {
        Boolean result = orderItemService.addOrderItem(orderItem);
        if (result) {
            return R.ok();
        }
        return R.error("添加失败");
    }

    @PostMapping("/updateOrderItem")
    public R updateOrderItem(@RequestBody OrderItem orderItem) {
        Boolean result = orderItemService.updateOrderItem(orderItem);
        if (result) {
            return R.ok();
        }
        return R.error("修改失败");
    }

    /**
     * 上传Excel文件
     */
    @PostMapping("/upload/excel")
    public R uploadExcel(MultipartFile file) {
        try {
            // 重点,此处第三个参数需要传入Service对象
            EasyExcel.read(file.getInputStream(), OrderItem.class, new OrderItemListener(orderItemService)).sheet().doRead();
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail();
        }
        return R.ok();
    }


    /**
     * 下载Excel文件
     * 注意,返回值是void
     */
    @GetMapping("/exportOrderItem")
    public void downloadExcel(HttpServletResponse response) throws IOException {
        //  查询数据库全部数据
        List<OrderItem> list = orderItemService.fuzzyQueryOrderItem(null);
        // 设置下载响应的格式
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        // 设置内容类型
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码
        String fileName = URLEncoder.encode("订单明细表", "UTF-8");
        response.setHeader("Content-disposition", "attachment;filename=" + fileName + ".xlsx");

        // easyexcel导出工具
        EasyExcel.write(response.getOutputStream(), OrderItem.class).autoCloseStream(Boolean.FALSE).sheet("订单明细")
                .doWrite(list);
    }

}
